#include <REGX52.H>
#include "Key.h"
#include "Delay.h"
#include "AT24C02.h"
#include "Nixie.h"
#include "Timer0.h"

unsigned char KeyNum,Min,Sec,MiniSec,Run_Flag;
void main(void)
{
	Timer0_Init();
	while(1)
	{
		KeyNum = Key();
		if (KeyNum == 1)Run_Flag = !Run_Flag;
		if (KeyNum == 2){Min = 0;Sec = 0;MiniSec = 0;}
		if (KeyNum == 3)
		{
			AT24C02_WriteByte(1,Min);
			Delay1ms(5);
			AT24C02_WriteByte(2,Sec);
			Delay1ms(5);
			AT24C02_WriteByte(3,MiniSec);
			Delay1ms(5);
		}
		if (KeyNum == 4)
		{
			Min = AT24C02_ReadByte(1);
			Sec = AT24C02_ReadByte(2);
			MiniSec = AT24C02_ReadByte(3);
		}
		Nixie_SetBuf(1,Min/10);
		Nixie_SetBuf(2,Min%10);
		Nixie_SetBuf(3,11);
		Nixie_SetBuf(4,Sec/10);
		Nixie_SetBuf(5,Sec%10);
		Nixie_SetBuf(6,11);
		Nixie_SetBuf(7,MiniSec/10);
		Nixie_SetBuf(8,MiniSec%10);
	}
}

void Sec_Loop(void)
{
	if (Run_Flag)
	{
		MiniSec++;
		if (MiniSec >= 100)
		{
			MiniSec = 0;
			Sec++;
			if (Sec >= 60)
			{
				Sec = 0;
				Min++;
				if (Min >= 60)
				{
					Min = 0;
					Min = 0;
				}
			}
		}
	}
	
}

void Timer0_Routine(void) interrupt 1
{
	static unsigned int T0Count1,T0Count2,T0Count3;
	//TH0寄存器
	TH0 = 0XFC;
	//TL0寄存器
	TL0 = 0X17 + 1;
	T0Count1++;	
	if (T0Count1 >= 20)
	{
		T0Count1 = 0;
		Key_Loop();
	}
	T0Count2++;
	if (T0Count2 >= 2)
	{
		T0Count2 = 0;
		Nixie_Loop();
	}
	T0Count3++;
	if (T0Count3 >= 10)
	{
		T0Count3 = 0;
		Sec_Loop();
	}
}
